public class BankruptcyChecker {
	private Player p, newOwner;
	private Block[] bl;
	private int worth;
	private Property[] prop;
	private Rail[] rail;
	private Utility[] util;
	private Property currentProp;
	private Utility currentUtil;
	private Rail currentRail;
	
	public BankruptcyChecker(Player p_, Block[] bl_ ){
		this.p=p_;
		this.bl=bl_;
		this.prop = new Property[22];
		this.rail = new Rail[4];
		this.util = new Utility[2];
		this.worth=0;
		
		if(this.bl[this.p.getCurrentPosition()] instanceof Property){
			this.currentProp = (Property)this.bl[this.p.getCurrentPosition()];
			this.newOwner = this.currentProp.getOwner();
		}else if(this.bl[this.p.getCurrentPosition()] instanceof Utility){
			currentUtil= (Utility)this.bl[this.p.getCurrentPosition()];
			this.newOwner = this.currentUtil.getOwner();
		}else if(this.bl[this.p.getCurrentPosition()] instanceof Rail){
			currentRail = (Rail)this.bl[this.p.getCurrentPosition()];
			this.newOwner = this.currentRail.getOwner();
		}
		
	}
	
	public int checkWorth(){
		
		int propCounter=0;
		int utilCounter=0;
		int railCounter=0;
		
	for(int i=0; i<bl.length; i++){
	
		if(bl[i] instanceof Property){
			this.prop[propCounter] = (Property)bl[i];
			propCounter++;
		}else if(bl[i] instanceof Utility){
			this.util[utilCounter] = (Utility)bl[i];
			utilCounter++;
		}else if(bl[i] instanceof Rail){
			this.rail[railCounter] = (Rail)bl[i];
			railCounter++;
		}
		
	}
	
	for(int i=0; i<prop.length; i++){
		if (prop[i].getOwner()==this.p && prop[i].isIsMortgaged()==false){
			this.worth=worth+prop[i].getMortgage();
		}	
	}
	
	for(int i=0; i<util.length; i++){
		if (util[i].getOwner()==this.p && util[i].isIsMortgaged()==false){
			this.worth=worth+util[i].getMortgage();
		}	
	}
	
	for(int i=0; i<rail.length; i++){
		if (rail[i].getOwner()==this.p && rail[i].isIsMortgaged()==false){
			this.worth=worth+rail[i].getMortgage();
		}	
	}
	
	this.worth=this.worth+p.getMoney();	
	return worth;
	}
	
	public void transferAssetsPlayer(){
		// Check if which property is owned by current Player, then transfer it to newOwner
		for(int i =0; i<prop.length; i++){	
			if(prop[i].getOwner()==this.p){
				prop[i].setOwner(newOwner);
			}
		}
		
		for(int i =0; i<util.length; i++){	
			if(util[i].getOwner()==this.p){
				util[i].setOwner(newOwner);
			}
		}
		
		for(int i =0; i<rail.length; i++){	
			if(rail[i].getOwner()==this.p){
				rail[i].setOwner(newOwner);
			}
		}
		this.newOwner.setMoney(this.p.getMoney());
		this.p.setMoney(0);
		this.p.setActive(false);
		
	}
	
	public void transferAssetsBank(){
		for(int i =0; i<prop.length; i++){	
			if(prop[i].getOwner()==this.p){
				prop[i].setOwner(null);
			}
		}
		
		for(int i =0; i<util.length; i++){	
			if(util[i].getOwner()==this.p){
				util[i].setOwner(null);
			}
		}
		
		for(int i =0; i<rail.length; i++){	
			if(rail[i].getOwner()==this.p){
				rail[i].setOwner(null);
			}
		}
		this.p.setMoney(0);
		this.p.setActive(false);
	}
	
}